﻿using Core.Framework;
using System.Data;
using System.Data.SqlClient;

namespace Core.DataAccess.Model
{
    public class SQLServer : IDBFactory
    {
        string defaultConnectionString { get; set; }

        public IDbConnection Create(string connectionString, string type)
        {
            var connection = new SqlConnection(connectionString);
            connection.Open();
            return connection;
        }

        public IDbConnection Create(bool isReader = false)
        {
            this.defaultConnectionString
                = AppSetting.GetConnectionString("MSSQL");
            var connection = new SqlConnection(defaultConnectionString);
            connection.Open();
            return connection;
        }

        public IDbConnection Create(string key, bool isReader = false)
        {

            if (!AppSetting.IsExist(key))
            {
                throw new System.Exception($"AppSetting: ConnectionStrings not isExist {key}");
            }

            this.defaultConnectionString
                = AppSetting.GetConnectionString(key);

            var connection = new SqlConnection(defaultConnectionString);
            connection.Open();
            return connection;
        }
    }
}
